/**
 * 
 */
package adc.app.ui.wicket.security;

import org.apache.wicket.Application;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;

import adc.app.spec.IAutheBs;
import adc.app.spec.IUser;
import adc.app.ui.wicket.IMsgs;
import adc.app.ui.wicket.LabeledLinkPanel;

@AuthenticationRequired
public class LogoutLink extends LabeledLinkPanel {
	private static final long serialVersionUID = 1L;
	
	@SpringBean IAutheBs<IUser> authBS;
	
	public LogoutLink(String id) {
		super(id, new ResourceModel(IMsgs.LOGOUT));
		this.setLink(new AjaxLink<Object>(this.getLinkId()) {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				if (authBS.isAuthenticated()) {
					authBS.logout();
				}
				target.appendJavaScript("window.location.href='"
					+ RequestCycle.get().urlFor(Application.get().getHomePage(), new PageParameters()).toString()
					+ "'");
			}
			
		});
	}
}